﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Messaging;

namespace _16_MSMQReader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void m_Read_Click(object sender, EventArgs e)
        {
            /*
            System.Messaging.Message msg = m_Queue.Receive();
            String text = (String)msg.Body;
            m_Text.Text += text + "\n";
            //*/

            m_Queue.BeginReceive(new TimeSpan(1, 2, 30, 24, 900), null, new AsyncCallback(OnReceive));
            
        }

        private void OnReceive(IAsyncResult ar)
        {
            System.Messaging.Message msg = m_Queue.EndReceive(ar);
            String text = (String)msg.Body;
            m_Text.Text += text + "\n";

        }

        private void m_ReadTransaction_Click(object sender, EventArgs e)
        {
            System.Messaging.MessageQueueTransaction transaction;
            transaction = new System.Messaging.MessageQueueTransaction();
            transaction.Begin();
            System.Messaging.Message msg = m_QueueTransaction.Receive(transaction);
            String text = (String)msg.Body;
            m_Text.Text += text + "\n";

            //Object a = null; a.ToString();
            transaction.Commit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_QueueXML.Formatter = new XmlMessageFormatter(new Type[]{typeof(String)});
            System.Messaging.Message msg = m_QueueXML.Receive();
            String text = (String)msg.Body;
            m_Text.Text += text + "\n";
        }
    }
}
